/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ChateoFacadeNI;

import ChateoController.ChatViewController;
import ourMessage.ConnexionUDPClient;
import ourMessage.Hello;
import ourMessage.ConnexionUDPServer;
import ourMessage.Bye;
import ChateoController.*;
import java.io.File;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import ourMessage.FileTransfertDemand;
import ourMessage.Message;

/**
 *
 * @author adrian
 * @author olga
 */
public class FacadeNI {

    private ChatViewController theController;
    private ConnexionUDPServer UDPServer;
    /* OBSELETE
     public FacadeNI(Controller theController, ConnexionUDPServer UDPServer, int port){
     this.theController=theController;
     this.UDPServer=UDPServer;
     }
     */
    
    /**
     * 
     * @param father
     * @param port
     * @throws SocketException 
     */

    public FacadeNI(ChatViewController father, int port) throws SocketException {
        this.theController = father;
        this.UDPServer = new ConnexionUDPServer(port, this);
    }

    public ChatViewController getTheController() {
        return theController;
    }

    public ConnexionUDPServer getUDPServer() {
        return UDPServer;
    }

    public void setTheController(ChatViewController theController) {
        this.theController = theController;
    }

    public void setUDPServer(ConnexionUDPServer UDPServer) {
        this.UDPServer = UDPServer;
    }

    public void incommingHello(String remoteLogin, InetAddress remoteAddr) throws UnknownHostException {
        theController.newUser(remoteLogin, remoteAddr);
    }

    public static void outgoingHello(InetAddress addr, String myLogin) throws UnknownHostException, SocketException {
        ConnexionUDPClient sender = new ConnexionUDPClient();
        sender.sendMessage(addr, new Hello(myLogin, false));
        System.out.println("The client " + myLogin + " has been connected. \n");

    }

    public void incommingBye(String remoteLogin, InetAddress remoteAddr) throws UnknownHostException {
        theController.userLeft(remoteLogin);
    }

    public void outgoingBye(InetAddress addr, String myLogin) throws UnknownHostException, SocketException {
        ConnexionUDPClient sender = new ConnexionUDPClient();
        sender.sendMessage(addr, new Bye(myLogin));

    }

    public void incommingAck(String remoteLogin, InetAddress remoteAddr) throws UnknownHostException {
        theController.newUserAck(remoteLogin, remoteAddr);
    }

    public void outgoingAck(InetAddress addr, String myLogin) throws UnknownHostException, SocketException {
        ConnexionUDPClient sender = new ConnexionUDPClient();
        sender.sendMessage(addr, new Hello(myLogin, true));
    }

    /**
     * *************************************************
     ******************Methods reserved for files only***
     *
     * @param remoteLogin is the login of the remote User we are asking the
     * agreement to send the file to
     * @param remoteAddr is the IP address of the remote User
     ************************************************
     */
    public void sendFileTransferRequest(String remoteLogin, InetAddress remoteAddr) {

    }

    /**
    This method is aimed in confirming a file reception
     This assumes that a file transfer was previously done on the other side
     * @param remoteLogin
     * @param remoteAddr
     * @param isAccepetd
     */
    public void sendFileTransferConfirmation(String remoteLogin, InetAddress remoteAddr, boolean isAccepetd) {

    }

    /*
     This method is aimed in deciding which decision 
     to take when a user receives a file
 
     */
    public void receivedFile(String localLogin, InetAddress localAddr, Message msg) {

    }

    /*
     Depending on the OS installed on the PC,this method 
     creates the a path to the received file
     */
    //TODO Method to eview after deciding where in the GUI we should put the getFilePath
    /*private String getFilePath() {
     String path=chatGUI.getFilePath();
        
        
     return path;
     }*/
    /*
     Starting to send a file to a remote User
     */
    public void startFileEmission(String remoteLogin, InetAddress remoteAddr, File file2Snd) {

    }

    /**
     * Strating to receive a file from a remote User
     *
     * @param remoteLogin
     * @param remoteAddr
     * @param msg is the file to receive
     */
    public void startFileReception(String remoteLogin, InetAddress remoteAddr, Message msg) {

    }

    /**
     * This method is aimed in really sending a file to a user It contains
     * different state machines
     *
     * @param remoteLogin can be null when moving to Canceled state.
     * @param remoteAddr can be null when moving to Canceled state.
     * @param file2Snd can be null when moving to Canceled state.
     * @param msg Generic message that can move the state machine to another
     * state. Can also be null when moving to Canceled state
     */
    public void fileEmission(String remoteLogin, InetAddress remoteAddr, File file2Snd, Message msg) {

    }

    /**
     * Enable file emission by creating a new thread.
     */
    private void enableFileEmission() {

    }
    
    /**
     * 
     * @param remoteLogin
     * @param remoteAddr
     * @param file2Snd
     * @param msg 
     */
    private void perfomSndFile(String remoteLogin, InetAddress remoteAddr,File file2Snd,Message msg){
        
    }
    /**
     * 
     * @param remoteLogin
     * @param remoteAddr
     * @param msg 
     */
    
    public void fileReception(String remoteLogin, InetAddress remoteAddr, FileTransfertDemand msg) {

    } 
    
    /**
     * 
     * @param remoteLogin
     * @param remoteAddr 
     */
    public void enableFileReception(String remoteLogin,InetAddress remoteAddr){
        
    }
    
    /**
     * This method is aimed in initializing access to the hard drive
     */
    public void performRcvFile(){
        
    }
    
    /**
     * 
     * This method is aimed in moving the incoming file's state to the appropriate state
     * @param state new File's state
     * @param remoteLogin
     * @param remoteAddr 
     */
    public void moveReceptionStateTo(FileTransfer state,String remoteLogin,InetAddress remoteAddr){
        
    }
    
    /**
     * 
     * This method is aimed in moving the outgoing file's state to the appropriate state
     * @param state
     * @param remoteLogin
     * @param remoteAddr 
     */
    public void moveEmissionStateTo(FileTransfer state,String remoteLogin,InetAddress remoteAddr){
        
    }
    
    /**
     * This method is aimed in ending a file emission transfer
     * by stopping the emission Thread and updating the GUI
     */
    public void finishFileEmission() {
        
    }
    
    /**
     * This method is aimed in ending a file reception transfer
     * by stopping the reception Thread and updating the GUI
     */
    public void finishFileReception() {
        
    }
    
    /**
     * This method is aimed in cancelling a file transfer
     * by stopping the emission Thread and updating the GUI
     */
    public void cancelEmissionTransfer(){
        
    }
    
    /**
     * This method is aimed in cancelling a file transfer
     * by stopping the reception Thread and updating the GUI
     */
    public void cancelReceptionTransfer(){
        
    }
    
}
